package guiHandler;
/**
 * 
 * @author Peter Steiner & Fredrik Wallenius
 *
 */
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

import adapterHandler.GuiSockAdapter;

public class ChatWindow extends JPanel implements ActionListener {
    /**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	
	
	private JTextField textField;
    private static JTextArea textArea;
    private GuiSockAdapter adapter;
    private final static String newline = "\n";

    public ChatWindow(GuiSockAdapter adapter_) {
        super(new GridBagLayout());
        
        adapter = adapter_;
        
        textArea = new JTextArea(40, 60);
        textArea.setEditable(false);
        JScrollPane scrollPane = new JScrollPane(textArea);
        
        textField = new JTextField(60);
        textField.addActionListener(this);

        //Add Components to this panel.
        GridBagConstraints c = new GridBagConstraints();
        c.gridwidth = GridBagConstraints.REMAINDER;

        c.fill = GridBagConstraints.BOTH;
        c.weightx = 1.0;
        c.weighty = 1.0;
        add(scrollPane, c);
        
        c.fill = GridBagConstraints.SOUTH;
        add(textField, c);
    }
    
    public JTextField getTextField() {
		return textField;
	}

	public JTextArea getTextArea() {
		return textArea;
	}
	
	public void appendTextArea(String str){
	    if(str != null){
        	str = str + newline;
 	        textArea.append(str);
	        textArea.setCaretPosition(textArea.getDocument().getLength());
	    }
	}

    public void actionPerformed(ActionEvent evt) {
        String text = textField.getText();
        textField.selectAll();
        adapter.writeToSocket(text);
    }
}
